package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.assistant.AssistantShopSet;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/11/11 17:29
 */
public class UserAssistantShopDaoImpl extends TempDataObjectDao<AssistantShopSet> {
    @Override
    protected boolean addData(AssistantShopSet data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_assistant_shop(userId,mallId,count) VALUES(?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getMallId());
            call.setLong(i++, data.getCount());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(AssistantShopSet data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_assistant_shop SET count=?  WHERE userId=? and mallId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getCount());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getMallId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected AssistantShopSet parseData(ResultSet rs) throws SQLException {
        AssistantShopSet data = new AssistantShopSet();
        data.setUserId(rs.getLong("userId"));
        data.setMallId(rs.getInt("mallId"));
        data.setCount(rs.getLong("count"));
        return data;
    }

    public Map<Integer, AssistantShopSet> getAssistantShopMap(long userId) {
        Map<Integer, AssistantShopSet> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_assistant_shop where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                AssistantShopSet data = parse(rs);
                dataMap.put(data.getMallId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
